package com.example.project22_editingmediaplayer;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.minimusicdemo.Main_play;
import com.example.project22_editingmediaplayer.adapter.RowSong;
import com.example.project22_editingmediaplayer.adapter.RowSongOfRating;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

public class PlayListByRating extends Activity {
	Context myContext;
	ListView listView_detail;
	ArrayList<SongObject> listSongAlbum;
	RowSongOfRating rowAdapterSongAlbum;
	private BroadcastReceiver DatabaseBroadcast = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(MainActivity.GET_SONG_BY_RATING)){
				listSongAlbum = intent.getParcelableArrayListExtra("value");				
			}
			Log.e("ratingReceiver",String.valueOf(listSongAlbum.size()));
			rowAdapterSongAlbum.resetData(listSongAlbum);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listplay_detail);
		myContext = getApplicationContext();
		
		listView_detail = (ListView) findViewById(R.id.lv_playlistdetail);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		rowAdapterSongAlbum = new RowSongOfRating(listSongAlbum, myContext);
		Log.e("adapter",String.valueOf(rowAdapterSongAlbum.getCount()));
		listView_detail.setAdapter(rowAdapterSongAlbum);
		listView_detail
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						SongResource.listSong.clear();
						SongResource.listSong.add((SongObject) listView_detail
								.getItemAtPosition(arg2));
						MainActivity.mService.PlaySong(0);
						Intent intent = new Intent(PlayListByRating.this,
								Main_play.class);
						MainActivity.mService.flag_first = false;
						startActivity(intent);

					}
				});
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		getMenuInflater().inflate(R.menu.menuplayemotion, menu);
		return true;
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		registerReceiver(DatabaseBroadcast, new IntentFilter(MainActivity.GET_SONG_BY_RATING));
		Intent it=new Intent();
		it.setAction(MainActivity.GET_SONG_BY_RATING);
		startService(it);
		
		Log.e("adapter1",String.valueOf(rowAdapterSongAlbum.getCount()));
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		unregisterReceiver(DatabaseBroadcast);
		super.onPause();
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		return super.onMenuItemSelected(featureId, item);
	}

	// xu ly back lai man hinh truoc
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case R.id.playall:
			// finish();
			try {
				SongResource.listSong.clear();
				SongResource.listSong = rowAdapterSongAlbum.getAllData();
				Intent intent = new Intent(PlayListByRating.this,
						Main_play.class);
				MainActivity.mService.PlaySong(0);
				MainActivity.mService.showNotification(0,
						SongResource.listSong.get(0));
				MainActivity.btnPlay
						.setImageResource(R.drawable.apollo_holo_light_pause);
				MainActivity.namesong.setText(SongResource.listSong.get(0)
						.get_name());
				MainActivity.singer.setText(SongResource.listSong.get(0)
						.get_artist());
				MainActivity.mService.flag_first = false;
				startActivity(intent);
			} catch (Exception e) {
				// TODO: handle exception
				Toast toast = Toast.makeText(myContext, "no song in list",
						Toast.LENGTH_LONG);
				toast.show();
			}

			break;

		default:
			break;
		}

		return true;
	}

}
